﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;

namespace DataAccess
{
    public class ClaimDataAccess
    {
        public string InsertClaim(ClaimInfo cl)
        {
            InsuranceSystemDataContext db = new InsuranceSystemDataContext();
            var claim = db.Claims.Where(c=>c.PolicyID == cl.PolicyID).SingleOrDefault();
            if (claim == null)
            {
                Claim c = new Claim();
                c.ClaimID = cl.ClaimID;
                c.PolicyID = cl.PolicyID;
                c.CustomerID = cl.CustomerID;
                c.ClaimDate = cl.ClaimDate;
                c.PlaceOfAccident = cl.PlaceOfAccident;
                c.DateOfAccident = cl.DateOfAccident;
                c.DescriptionDamage = cl.DescriptionDamage;

                db.Claims.InsertOnSubmit(c);
                db.SubmitChanges();
                return "Claim successful";
            }
            else
            {
                return "Failed! \nThis Policy is claimed before";
            }
            
        }

        public List<ClaimInfo> getAllClaim()
        {
            InsuranceSystemDataContext db = new InsuranceSystemDataContext();
            var result = from claim in db.Claims
                         join c in db.Customers on claim.CustomerID equals c.CustomerID
                         select new
                         ClaimInfo
                         {
                             ClaimID = claim.ClaimID,
                             PolicyID = Convert.ToInt32( claim.PolicyID),
                             CustomerID = Convert.ToInt32(claim.CustomerID),
                             CustomerName = c.Firstname + " " + c.Lastname,
                             ClaimDate = Convert.ToDateTime(claim.ClaimDate),
                             PlaceOfAccident = claim.PlaceOfAccident,
                             DateOfAccident = Convert.ToDateTime(claim.DateOfAccident),
                             DescriptionDamage = claim.DescriptionDamage
                         };
            return result.ToList();
        }

        public List<ClaimInfo> getListClaimByDateClaimDate(DateTime startDate, DateTime endDate)
        {
            InsuranceSystemDataContext db = new InsuranceSystemDataContext();
            var result = from claim in db.Claims
                         join c in db.Customers on claim.CustomerID equals c.CustomerID
                         where claim.ClaimDate>= startDate && claim.ClaimDate<= endDate
                         select new
                         ClaimInfo
                         {
                             ClaimID = claim.ClaimID,
                             PolicyID = Convert.ToInt32(claim.PolicyID),
                             CustomerID = Convert.ToInt32(claim.CustomerID),
                             CustomerName = c.Firstname + " " + c.Lastname,
                             ClaimDate = Convert.ToDateTime(claim.ClaimDate),
                             PlaceOfAccident = claim.PlaceOfAccident,
                             DateOfAccident = Convert.ToDateTime(claim.DateOfAccident),
                             DescriptionDamage = claim.DescriptionDamage
                         };
            return result.ToList();
        }

        public List<ClaimInfo> getListClaimByDateAccidentDate(DateTime startDate, DateTime endDate)
        {
            InsuranceSystemDataContext db = new InsuranceSystemDataContext();
            var result = from claim in db.Claims
                         join c in db.Customers on claim.CustomerID equals c.CustomerID
                         where claim.DateOfAccident >= startDate && claim.DateOfAccident <= endDate
                         select new
                         ClaimInfo
                         {
                             ClaimID = claim.ClaimID,
                             PolicyID = Convert.ToInt32(claim.PolicyID),
                             CustomerID = Convert.ToInt32(claim.CustomerID),
                             CustomerName = c.Firstname + " " + c.Lastname,
                             ClaimDate = Convert.ToDateTime(claim.ClaimDate),
                             PlaceOfAccident = claim.PlaceOfAccident,
                             DateOfAccident = Convert.ToDateTime(claim.DateOfAccident),
                             DescriptionDamage = claim.DescriptionDamage
                         };
            return result.ToList();
        }
    }
}
